package hzst.android.form.view;

import hzst.android.R;
import hzst.android.form.ViewCreator;
import hzst.android.form.info.BaseViewInfo;
import hzst.android.form.info.SelectViewInfo;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
/**
 * 选择控件。和{@link #FMSpinner} {@link #FMCheckGroup}不一样，
 * 它是从新的页面中选择一项或多项数据；并且数据源不是直接从{@link #BaseViewInfo.SubmitValue}获取，
 * 而是通过调用{@link #BaseViewInfo.SubmitValue}中的接口获取数据源列表。
 * @author wt
 *
 */
public class FMSelectView extends FMBaseView{
	
	private TextView tvContent;
	private RelativeLayout rlayAdd,rlayDelete;
	
	private SelectViewInfo info = new SelectViewInfo();
	private String ids;
	private String names;
//	private OnAddClickListener onAddClickListener;
	
	public static int currIndex;
	
	public FMSelectView(Context context) {
		super(context);
		initView();
	}
	
	public void setIds(String ids) {
		this.ids = ids;
	}

	public void setNames(String names) {
		this.names = names;
		tvContent.setText(names);
	}

	
	@Override
	public void setInfo(BaseViewInfo info,ViewCreator viewCreator) {
		super.setInfo(info, viewCreator);
		this.info = (SelectViewInfo)info;
		tvContent.setText(submitValues.get(0).getValue());
		if(info.isReadOnly()){
			tvContent.setEnabled(false);
			rlayAdd.setVisibility(View.INVISIBLE);
			rlayDelete.setVisibility(View.INVISIBLE);

		}
	}
	
	protected void initView(){
		View view = LayoutInflater.from(context).inflate(R.layout.view_selectview, this);
		
		tvContent = (TextView) view.findViewById(R.id.tv_content);
		tvContent.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {}
			
			@Override
			public void afterTextChanged(Editable s) {
				switch (info.getPassType()) {
				case SelectViewInfo.PASS_TYPE_ALL:
					viewCreator.setSubmitValue(names, info, 0);
					viewCreator.setSubmitValue(ids, info, 1);
					break;
				case SelectViewInfo.PASS_TYPE_NAME:
					viewCreator.setSubmitValue(names, info, 0);
					break;
				case SelectViewInfo.PASS_TYPE_ID:
					viewCreator.setSubmitValue(ids, info, 0);
					break;
				}
//				viewCreator.updateViewInfo(info, true);
			}
		});
		rlayAdd = (RelativeLayout) view.findViewById(R.id.rlay_add);
		rlayDelete = (RelativeLayout) view.findViewById(R.id.rlay_delete);
		rlayAdd.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
//				currIndex = (int) getTag();
//				Intent intent = null;
//				Bundle bundle = new Bundle();
//				switch (info.getSelectSource()) {
//				case SelectViewInfo.SOURCE_SCIENTIFIC:
//					intent = new Intent(context,ComprehensiveSelectTaskActivity.class);
//					break;
//				case SelectViewInfo.SOURCE_MEMBER:
//					intent = new Intent(context, AddressBookActivity.class);
//					if(info.isMultiple()){
//						bundle.putInt("status", AddressBookActivity.STATUS_MULTIPLE_CHOICE);
//					}else{
//						bundle.putInt("status", AddressBookActivity.STATUS_SINGLE_CHOICE);
//					}
//					bundle.putString("title", "选择人员");
//					intent.putExtras(bundle);
//					break;
//				default:
//					intent = new Intent(context,SelectSourceListActivity.class);
//					bundle.putSerializable("viewInfo", info);
//					intent.putExtras(bundle);
//					break;
//				}
//				if(intent != null){
//					((Activity)context).startActivityForResult(intent, 1);
//				}
			}
		});
		rlayDelete.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				tvContent.setText("");
				ids = "";
				names = "";
			}
		});
	}
	
	public interface OnAddClickListener{
		void add();
	}
	
}
